package com.ttbj.controller.refund;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.RefundDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.refund.RefundService;
import com.ttbj.util.ViewDataUtil;


@Controller
public class RefundController {
	
	@Autowired
	private RefundService refundService;
	
	/**
	 * 退款记录页面
	 * @return
	 */
	@RequestMapping(value="/refund/refund_list.htm")
	public String refund_list() {
		return "refund/refund_list";
	}
	
	/**
	 * 退款操作页面
	 * @return
	 */
	@RequestMapping(value="/refund/refund_tk.htm")
	public String refund_info() {
		return "refund/refund_tk";
	}
	
	/**
	 * 退款操作
	 * @param request
	 * @param orgMerOrderNum 
	 * @param refundAmtStr
	 * @return
	 */
	@RequestMapping(value="/refund/carriageRefund.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String carriageRefund(HttpServletRequest request, @RequestParam String orgMerOrderNum, @RequestParam String refundAmtStr) {
		try {
			this.refundService.refundTransaction(orgMerOrderNum, refundAmtStr);
			return ViewDataUtil.buildSuccessJson("退款成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 条件分页查询退款记录
	 * @param request
	 * @param refundDto 条件查询的退款类
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/refund/getRefundInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getRefundInfoByPage(HttpServletRequest request, RefundDto refundDto, Page page) {
		try {
			List<RefundDto> refundDtos = this.refundService.findRefundInfoByPage(refundDto, page);
			int count = 0;
			if (null != refundDtos) {
				count = this.refundService.findRefundInfoByPageCount(refundDto);
			}
			return ViewDataUtil.buildSuccessPageJson("退款记录", refundDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
